import java.awt.List;
import java.io.File;
import java.util.ArrayList;

import CA.CrashSuper;
import CA.FileInfo;
import CA.Helper;
import CA.Scrape_Helper;
import StringComparision.StringDistances;
import java.io.File;
import java.util.ArrayList;

import CA.CrashSuper;
import CA.FileInfo;
import CA.Helper;

import com.meterware.httpunit.GetMethodWebRequest;
import com.meterware.httpunit.HTMLElement;
import com.meterware.httpunit.HttpUnitOptions;
import com.meterware.httpunit.WebConversation;
import com.meterware.httpunit.WebLink;
import com.meterware.httpunit.WebRequest;
import com.meterware.httpunit.WebResponse;
import com.meterware.httpunit.WebTable;
import com.wcohen.ss.*;


public class test {

	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		test t = new test();
		String URL="https://crash-stats.mozilla.com/report/list?range_value=7&range_unit=days&date=2012-01-24&signature=gbmzh_bb.dll%400x11247&version=Firefox%3A9.0.1";
		String URL2="https://crash-stats.mozilla.com/report/list?range_value=7&range_unit=days&date=2012-01-26&signature=WrappedNativeMarker&version=Firefox%3A10.0b5";
		
		String URL3="https://crash-stats.mozilla.com/report/index/505a6f2a-2b3c-4390-8152-7450b2120123";
		
		String URL4="https://crash-stats.mozilla.com/report/index/9ec2822e-a62a-4406-a925-c9cd62120128";
		String URL5="https://crash-stats.mozilla.com/report/index/4fd68a2e-df80-49f9-b623-a2d612120128";	
		
		String URL6="https://crash-stats.mozilla.com/report/index/dd3d8e56-14f5-4918-91ac-43f4d2120128";
		String URL7="https://crash-stats.mozilla.com/report/index/3c694f5a-0ff7-41f9-98aa-ea2c32120128";
		String URL8="https://crash-stats.mozilla.com/report/index/6626eac7-e01b-4012-8d86-48dd22120128";
		String URL9="https://crash-stats.mozilla.com/report/index/9e7518c1-c9dc-4a0f-ab27-f9a1f2120128";
		String URL10="https://crash-stats.mozilla.com/report/index/40dce204-b1e3-4b2e-8972-fe01d2120128";
		String URL11="https://crash-stats.mozilla.com/report/index/1858193f-50a7-4fa8-9093-076672120125";
		
		t.Run2(URL11);
		
		
		/*
		t.Run2(URL4);
		
		
		
		t.Run2(URL5);
		t.Run2(URL6);
		t.Run2(URL7);
		t.Run2(URL8);
		t.Run2(URL9);
		t.Run2(URL10);
		*/
	}
	
	
	private WebTable[] getCrashTable(String URL)
	{	
		// test getting specific information about the page
		HttpUnitOptions.setScriptingEnabled(false);	
		WebTable[] tables = null;
	    try {
	    	WebConversation wc = new WebConversation();
	    	WebRequest request = new GetMethodWebRequest( URL );
	    	WebResponse response = wc.getResponse( request );
	    	tables = response.getTables();
	    	
	//    	System.out.println(getNumberOfRelatedBugs(tables));

	    } catch (Exception e) {
		       System.err.println( "Exception: " + e );
		    }
		
		return tables;
	}
	
	
	private int getNumberOfRelatedBugs(WebTable[] tables){
		int retVal=0;
		System.out.println(tables[0].toString());
		System.out.println(tables[1].toString());
		System.out.println(tables[2].toString());
		System.out.println(tables[3].toString());
		System.out.println(tables[4].toString());
		System.out.println(tables[5].toString());
		System.out.println(tables[6].toString());
		System.out.println(tables[7].toString());
		System.out.println(tables[8].toString());
		
		
		return retVal;
	}
	
	
	private String getAllCrashMetaStats(String URL){
		String retVal="";
		WebTable[] tables = getCrashTable(URL);
		Scrape_Helper sh = new Scrape_Helper();
		
		retVal+="Last Crash:" + sh.convertInputToSeconds(getTableVal("Last Crash",tables))+"\n";
		retVal+="Install Age:"  + sh.convertInputToSeconds(getTableVal("Install Age",tables))+"\n";
		retVal+="Date Processed:" + getTableVal("Date Processed",tables) + "\n"; 
		retVal+="Uptime:" + getTableVal("Uptime",tables) + "\n";
		retVal+="Install Time:" + getTableVal("Install Time",tables) + "\n";
		retVal+="Crash Reason:" + getTableVal("Crash Reason",tables) + "\n";
		retVal+="Release Channel:" + getTableVal("Release Channel",tables) + "\n";
		retVal+="Product:" + getTableVal("Product",tables) + "\n";
		retVal+="OS:" + getTableVal("OS",tables) + "\n";
		
	//	# Related bugs
		//retVal+="Date Processed:" + getTableVal("Daate Processed",tables) + "\n";
				
		return retVal;
	}
	
	
	
	
	
	private String getTableVal(String searchVal, WebTable[] tables){
		int LocationVal=getTableLocation(searchVal, tables);
		String retVal="NaN";
		if (LocationVal !=-1){
			retVal=tables[0].getCellAsText(LocationVal, 1);
		}
		return retVal;
	}
	
	
	private int getTableLocation(String val, WebTable[] tables)
	{
		int retVal=-1;
		for (int i = 0; i < tables[0].getRowCount(); i++) {
			if (tables[0].getCellAsText(i, 0).equals(val)){
				retVal=i;
			}
		}
		return retVal;
	}
	
	
	
	private void Run2(String URL)
	{
		System.out.println(getAllCrashMetaStats(URL));
	//	getCrashInfo(URL);	
		
	}
	
	
	private int getCrashCount(String input)
	{
		return Integer.parseInt(input.substring(0, input.indexOf("-")).replace(" ", ""));
	}

	
	// Get the number of pages that will need to be looped through
	private int getLoopPageCount(int CrashCount){
		final int maxCrashPerPage = 250;
		int PageCount = 0;
		if (CrashCount >0){
			
			if (CrashCount <= maxCrashPerPage){
				PageCount=1;
			}else{
				
				PageCount=(CrashCount / 250);
				if ((CrashCount % maxCrashPerPage) > 0){
					PageCount = PageCount+1;
				}	
			}	
		}
		return PageCount;
	}
	
	
	
	
	public void Run(String URL)
	{
	//49561
		
	//	System.out.println(getLoopPageCount(getCrashCount("175 - 100.000%")));
	//	System.out.println(getLoopPageCount(getCrashCount("49561 - 1asdfas.000%")));
	//	System.out.println(getLoopPageCount(175));
	
		
		

		String Info="";	
		HttpUnitOptions.setScriptingEnabled(false);	
	    try {
	    	WebConversation wc = new WebConversation();
	    	WebRequest request = new GetMethodWebRequest( URL );
	    	WebResponse response = wc.getResponse( request );
	    	
	   
	    	
	    //	String[] da = response.getElementNames();
	    	WebLink[] da = response.getLinks();
	    //	System.out.println(da.length);
	    	
	    	
	    	
	    	
	    	for (int i = 0; i < da.length; i++) {
	    	
	//    		System.out.println(da[i].getTarget());
	    	if (da[i].getText().toLowerCase().contains("next")){
	    		System.out.println(i);
	    	
	    		System.out.println(da[1531].getID());
	    		System.out.println(da[1531].getName());
	    		System.out.println(da[1531].getPageFrame());
	    		System.out.println(da[1531].getTagName());
	    		System.out.println(da[1531].getTarget());
	    		System.out.println(da[1531].getText());
	    		System.out.println(da[1531].getTitle());
	    		System.out.println(da[1531].getURLString()); //URL of current page
	    		System.out.println(da[1531].MATCH_URL_STRING);
	    		System.out.println(da[1531].getFragmentIdentifier());
	    		System.out.println("--------------------------");
	    	}
	    	}
	    		
	    		//	System.out.println(da[i].getURLString());
	    	//	System.out.println(da[i].get);
	  //  	}
	    	
	    	
	    	/*
	    	int i=0;
	    	WebTable[] tables = response.getTables();
	    	System.out.println(tables[0].toString());
	    	System.out.println(tables[1].toString());
	    	System.out.println(tables[2].toString());
	    	System.out.println(tables[3].toString());
	    	System.out.println(tables[4].toString());
	    	System.out.println(tables[5].toString());
	    	System.out.println(tables[6].toString());
		   */ 	
	  //DK  	String columnValue=tables[3].getCellAsText(1, 2);
	 //DK   	System.out.println(getCrashCount(columnValue));
	    	
	    //	System.out.println("Here" + tables[3].toString());	
	    	
	    	
	    //	System.out.println(tables[4].getRowCount()-1); // get the number of rows in the page
	    	
	    } catch (Exception e) {
		       System.err.println( "Exception: " + e );
		    }
	   // System.out.println(Info);	

	
	
	}
}



// Save these for unit tests
//System.out.println(getLoopPageCount(250));//1
//System.out.println(getLoopPageCount(251));//2
//System.out.println(getLoopPageCount(499));//2
//System.out.println(getLoopPageCount(500));//2
//System.out.println(getLoopPageCount(501));//3
//System.out.println(getLoopPageCount(508));//3
//System.out.println(getLoopPageCount(49561)); //199
